package com.rjyb.student_management_platform.service.Impl;

import com.rjyb.student_management_platform.dao.LoginDao;
import com.rjyb.student_management_platform.pojo.*;
import com.rjyb.student_management_platform.pojo.Class;
import com.rjyb.student_management_platform.service.LoginService;
import com.rjyb.student_management_platform.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.Model;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService{

    @Autowired
    LoginDao loginDao;

    @Autowired
    TeacherService ts;

    //登录验证方法
    @Override
    public String login(String code, Model model, String name, String password, HttpSession session, HttpServletRequest request) {
        String vcode = (String) request.getSession().getAttribute("VerifyCode");
        Student student  = loginDao.login1(name,password);
        Teacher teacher = loginDao.login2(name,password);

        if(student!=null && code.equalsIgnoreCase(vcode)){
            session.setAttribute("student",student);
            selectindex(model,session);
            Student ss = (Student) request.getSession().getAttribute("student");
            System.out.println(ss.getName());
            return "student/index";
        }else if(teacher!=null && code.equalsIgnoreCase(vcode)) {
            session.setAttribute("teacher",teacher);
         /*   List<Class> classList = ts.selectClass(session, model);
            model.addAttribute("classList",classList);
            model.addAttribute("stu",null);*/
            long tid = teacher.getTid();
            long typy = teacher.getType();
            if (typy==2){
                //查看班级作业完成率
                List<Map> list = ts.selectClassGuideCompletionRate(tid);
                model.addAttribute("list2",list);
                return "teacher/index";
            }else {
                //查看班级作业完成率
                List<Map> list = ts.selectCompletionRate(tid);
                model.addAttribute("list2", list);
                return "teacher/index";
            }
        }else {
            model.addAttribute("error","登陆失败");
            return "teacher/lyear_pages_login";
        }
    }


    //查询本人考勤和作业已完成或未完成
    public void selectindex( Model model, HttpSession session){
        Student student1 = (Student) session.getAttribute("student");
        int sid = (int) student1.getSid();
        int selectatt1 = loginDao.selectatt1(sid);
        model.addAttribute("selectatt1",selectatt1);
        int selectatt2 = loginDao.selectatt2(sid);
        model.addAttribute("selectatt2",selectatt2);
        int selectatt3 = loginDao.selectatt3(sid);
        model.addAttribute("selectatt3",selectatt3);
        int selectatt4 = loginDao.selectatt4(sid);
        model.addAttribute("selectatt4",selectatt4);
        List<Map> list = loginDao.selectans(sid);
        model.addAttribute("list1",list);
        int cid = loginDao.selectclass(sid);
        List<Jobtable> list1 = loginDao.selectans1(sid,cid);
        model.addAttribute("list2",list1);
    }
    //已到次数

    @Override
    public int selectatt1(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        int sid = (int) student.getSid();
        return loginDao.selectatt1(sid);
    }
    //未到次数

    @Override
    public int selectatt2(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        int sid = (int) student.getSid();
        return loginDao.selectatt2(sid);
    }
    //迟到次数

    @Override
    public int selectatt3(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        int sid = (int) student.getSid();
        return loginDao.selectatt3(sid);
    }
    //请假次数

    @Override
    public int selectatt4(HttpSession session) {
        Student student = (Student) session.getAttribute("student");
        int sid = (int) student.getSid();
        return loginDao.selectatt4(sid);
    }

}
